﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Bing.Service;

namespace Microsoft.Bing
{
    internal class SearchRequestor
    {
        /// <summary>
        /// Maximum offset is 1000
        /// </summary>
        /// <remarks>
        /// http://msdn.microsoft.com/en-us/library/dd250920.aspx
        /// </remarks>
        private const uint MaximumOffset = 1000;

        /// <summary>
        /// Maximum count is 50
        /// </summary>
        /// <remarks>
        /// http://msdn.microsoft.com/en-us/library/dd251004(v=MSDN.10).aspx
        /// </remarks>
        private const uint MaximumCount = 50;

        internal static void DecorateWebRequest(SearchRequest request)
        {
            request.Sources = new SourceType[] { SourceType.Web };

            // Web-specific request fields (optional)
            request.Web = new Microsoft.Bing.Service.WebRequest();
            request.Web.Count = Math.Min(SearchEnvironment.Default.PageSize, MaximumCount);
            request.Web.CountSpecified = true;
            request.Web.Offset = Math.Min(MaximumOffset, (SearchEnvironment.Default.PageSize * Math.Max(0, (SearchEnvironment.Default.CurrentPage - 1))));
            request.Web.OffsetSpecified = true;
            request.Web.FileType = (String.IsNullOrEmpty(SearchEnvironment.Default.FileTypeFilter) ? String.Empty : SearchEnvironment.Default.FileTypeFilter);
            request.Web.Options = new WebSearchOption[]
            {
                WebSearchOption.DisableHostCollapsing
            };
        }

        internal static void DecorateImageRequest(SearchRequest request)
        {
            request.Sources = new SourceType[] { SourceType.Image };

            request.Image = new ImageRequest();
            request.Image.Count = SearchEnvironment.Default.PageSize;
            request.Image.CountSpecified = true;
            request.Image.Offset = (SearchEnvironment.Default.PageSize * Math.Max(0, (SearchEnvironment.Default.CurrentPage - 1)));
            request.Image.OffsetSpecified = true;
        }

        internal static void DecorateVideoRequest(SearchRequest request)
        {
            request.Sources = new SourceType[] { SourceType.Video };

            request.Video = new VideoRequest();
            request.Video.SortBy = VideoSortOption.Relevance;
            request.Video.Count = SearchEnvironment.Default.PageSize;
            request.Video.CountSpecified = true;
            request.Video.Offset = (SearchEnvironment.Default.PageSize * Math.Max(0, (SearchEnvironment.Default.CurrentPage - 1)));
            request.Video.OffsetSpecified = true;
        }

        internal static void DecorateNewsRequest(SearchRequest request)
        {
            request.Sources = new SourceType[] { SourceType.News };

            // News-specific request fields (optional)
            request.News = new NewsRequest();
            request.News.Offset = (SearchEnvironment.Default.PageSize * Math.Max(0, (SearchEnvironment.Default.CurrentPage - 1)));
            request.News.OffsetSpecified = true;

            // The following request fields are mutually exclusive.
            // Uncomment the lines corresponding to the request field you wish
            // to use.
            if (!String.IsNullOrEmpty(SearchEnvironment.Default.LocationOverride))
                request.News.LocationOverride = SearchEnvironment.Default.LocationOverride;
            else if (!String.IsNullOrEmpty(SearchEnvironment.Default.NewsCategory))
                request.News.Category = SearchEnvironment.Default.NewsCategory;

            request.News.SortBy = SearchEnvironment.Default.NewsSort;
            request.News.SortBySpecified = true;
        }

        internal static void DecoratePhonebookRequest(SearchRequest request)
        {
            request.Sources = new SourceType[] { SourceType.Phonebook };

            // Phonebook-specific request fields (optional)
            request.Phonebook = new PhonebookRequest();
            request.Phonebook.Count = SearchEnvironment.Default.PageSize;
            request.Phonebook.CountSpecified = true;
            request.Phonebook.Offset = (SearchEnvironment.Default.PageSize * Math.Max(0, (SearchEnvironment.Default.CurrentPage - 1)));
            request.Phonebook.OffsetSpecified = true;
            request.Phonebook.FileType = SearchEnvironment.Default.FileTypeFilter;
            request.Phonebook.SortBy = SearchEnvironment.Default.PhonebookSort;
            request.Phonebook.SortBySpecified = true;
        }
    }
}
